﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class UpgradeController {

    private static SlowMotionUpgrade slowMotionUpgrade = null;
    private static GameObject selectedButton = null;

    private static UpgradeController _instance = null;
    public static UpgradeController Instance
    {
        get
        {
            if (_instance == null)
                _instance = new UpgradeController();
            return _instance;
        }
    }

    public void Initialization()
    {
        slowMotionUpgrade = new SlowMotionUpgrade();
        slowMotionUpgrade.Initialization();
    }

    public int GetCurrentLevel(GameObject go)
    {
        if (go.name.Contains("SlowMotion"))
            return slowMotionUpgrade.GetCurrentLevel();
        else return 0;
    }

    public int GetHighestChoosableLevel(GameObject go)
    {
        if (go.name.Contains("SlowMotion"))
            return slowMotionUpgrade.GetHighestChoosableLevel();
        else 
            return 0;
    }

    public GameObject GetSelectedButton()
    {
        return selectedButton;
    }

    public void SetSelectedButton(GameObject go)
    {
        selectedButton = go;
    }

    public void ApplyLevel()
    {
        if(selectedButton.transform.parent.gameObject.name.Contains("SlowMotion"))
            slowMotionUpgrade.SetCurrentLevel(int.Parse(selectedButton.GetComponentInChildren<Text>().text));
    }


    private sealed class SlowMotionUpgrade : UpgradeController 
    {
        private static int highestChoosableLevel = 0;
        private static int currentLevel = 0;
        private static int maxLevel = 0;
        private static string currentLevelKey = "currentslowmolevel";
        private static string highestChoosableLevelKey = "highestchoosableslowmolevel";

        public int GetHighestChoosableLevel()
        {
            return highestChoosableLevel;
        }

        public int GetCurrentLevel()
        {
            return currentLevel;
        }
        public void SetCurrentLevel(int level)
        {
            PlayerPrefs.SetInt(currentLevelKey, level);
            currentLevel = PlayerPrefs.GetInt(currentLevelKey);
        }

        public int GetMaxLevel()
        {
            return maxLevel;
        }

        public void Initialization()
        {
            if (PlayerPrefs.GetInt(currentLevelKey) == 0)
                PlayerPrefs.SetInt(currentLevelKey, 1);
            currentLevel = PlayerPrefs.GetInt(currentLevelKey);

            if (PlayerPrefs.GetInt(highestChoosableLevelKey) == 0)
                PlayerPrefs.SetInt(highestChoosableLevelKey, 3);
            highestChoosableLevel = PlayerPrefs.GetInt(highestChoosableLevelKey);

            Debug.Log("curLevel: " + PlayerPrefs.GetInt(currentLevelKey));
            Debug.Log("HighLevel: " + PlayerPrefs.GetInt(highestChoosableLevelKey));
            Debug.Log("HighLevel: " + highestChoosableLevel);

            PlayerPrefs.Save();
        }
    }

    //// Use this for initialization
    //void Start () {
	
    //}
	
    //// Update is called once per frame
    //void Update () {
	
    //}
}
